﻿using System;
using System.Collections.Generic;
using UnityEngine;
public class Server : BaseManager<Server>, IServer
{
    IClient _client;
    IDataBase _db;
    public IDataBase DB { get { return _db; } }
    public Player CurPlayer;
    //消息类型 消息解析函数
    Dictionary<Type, Action<Cmd>> _parser = new Dictionary<Type, Action<Cmd>>();

    public Server()
    {
        _db = SQliteMgr.GetInstance();
        _db.Init();

        _parser.Add(typeof(LoginCmd), ServerCmdParser.OnLogin);
        _parser.Add(typeof(RoleIndexCmd), ServerCmdParser.OnCreateScene);
        _parser.Add(typeof(JumpToCmd), ServerCmdParser.OnJumpTo);
    }

    public void Connect(IClient client)
    {
        _client = client;
    }


    public void ReceiveCmd(Cmd cmd)
    {
        //Debug.Log("服务器收到消息：" + cmd.GetType());
        Action<Cmd> func;
        if(_parser.TryGetValue(cmd.GetType(),out func))
        {
            if (func != null) 
            { 
                func(cmd); 
            }
        }
    }

    public void SendCmd(Cmd cmd)
    {
        _client.ReceiveCmd(cmd);
    }
}
